package com.tr.rentcar.controller.loginandregister;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

@WebServlet("/sendcode")
public class SendCodeController extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	/**
	 * 发送验证码
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String phone=req.getParameter("phone");  
		System.out.println("手机号码："+phone);
        resp.setCharacterEncoding("UTF-8");   
        //获取验证码  
        String code = getCode(req);  
        System.out.println("验证码："+code);
        //发送短信  
       sendMsg(phone,code);  
        resp.getWriter().write("true");     
        resp.flushBuffer(); 
	}
	/**
	 * 验证
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String code = req.getParameter("code");      
        resp.setCharacterEncoding("UTF-8");   
        if(req.getSession().getAttribute("code").equals(code)){  
            resp.getWriter().write("true");     
        }else {  
            resp.getWriter().write("false");     
        }  
        resp.flushBuffer(); 
	}
	
	/**
	 * 获取验证码  
	 * @param request
	 * @return
	 */
    private String getCode(HttpServletRequest request) {  
        Random rand =new Random();  
        String code = "";  
        for(int i=0;i<4;i++) {  
            code += rand.nextInt(10);  
        }  
        request.getSession().setAttribute("code",code);  
        return code;  
    }  
    /**
     * 发送短信
     * @param phone
     * @param code
     * @throws HttpException
     * @throws IOException
     */
    private void sendMsg(String phone,String code) throws HttpException, IOException {  
        HttpClient client = new HttpClient();  
        PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn/");  
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码  
        NameValuePair[] data ={   
                new NameValuePair("Uid", "49June"),// 注册的用户名    
                new NameValuePair("Key", "9561235cc6e0f7f4f7a7"),// 注册成功后，登录网站后得到的密钥    
                new NameValuePair("smsMob",phone),// 手机号码    
                new NameValuePair("smsText","验证码："+code)};// 短信内容    
        post.setRequestBody(data);  
  
        client.executeMethod(post);  
        Header[] headers = post.getResponseHeaders();  
        int statusCode = post.getStatusCode();  
        System.out.println("状态码statusCode:"+statusCode);  
        /*for(Header h : headers)  
        {  
            System.out.println("-----"+h.toString());  
        } */ 
        String result = new String(post.getResponseBodyAsString().getBytes("utf8"));  
        System.out.println("返回状态码："+result); //打印返回消息状态  
        post.releaseConnection();  
    }  

}
